/*
 * Copyright (C) 2012 BARBOTIN Nicolas
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "GCRoomWidget.h"

GCRoomWidget::GCRoomWidget(QWidget *parent) :
    QWidget(parent)
{
    gridSize = 32;
    layer = 8;
    isAdding = true;
    hasBg = false;
    hasSpr = false;
}

void GCRoomWidget::paintEvent(QPaintEvent *)
{
    QPainter p(this);
    p.setPen(Qt::NoPen);

    if(!hasBg) {
        QBrush b(QColor(255, 255, 255));
        p.setBrush(b);

        p.drawRect(0, 0, width(), height());
    } else {
        if(bg.isColor) {
            QBrush b(bg.color);
            p.setBrush(b);

            p.drawRect(0, 0, width(), height());
        } else {
            if(bg.isRepeated) {
                QBrush b(bg.image);
                p.setBrush(b);

                p.drawRect(0, 0, width(), height());
            } else
                p.drawImage(0, 0, bg.image);
        }
    }

    p.setPen(QColor(0, 0, 0));

    if(gridSize > 1) {
        for(int x = 1; x < width() / gridSize + 1; x++)
            p.drawLine(x * gridSize, 0, x * gridSize, height());

        for(int y = 1; y < height() / gridSize + 1; y++)
            p.drawLine(0, y * gridSize, width(), y * gridSize);
    }

    for(int i = 0; i < objPos.count(); i++) {
        if(objPos[i].z == layer) {
            p.drawImage(objPos[i].x , objPos[i].y, sprites[i]);
        }
    }
}

void GCRoomWidget::mouseReleaseEvent(QMouseEvent *e)
{
    if(e->button() != Qt::LeftButton)
        return;

    for(int y = 0; y < height() / gridSize; y++) {
        for(int x = 0; x < width() / gridSize; x++) {
            if(e->x() >= x * gridSize && e->x() <= x * gridSize + gridSize &&
                    e->y() >= y * gridSize && e->y() <= y * gridSize + gridSize) {
                short objs = -1;
                for(int i = 0; i < objPos.count(); i++) {
                    if(objPos[i].z == layer && x * gridSize >= objPos[i].x && x * gridSize <= objPos[i].x + sprites[i].width() &&
                            y * gridSize >= objPos[i].y && y * gridSize <= objPos[i].y + sprites[i].height()) {
                        objs = i;
                        break;
                    }
                }

                if(isAdding) {
                    if(!hasSpr)
                        return;

                    if(objs != -1 || currentSprite.isNull())
                        return;

                    PosXYZ pos = {x * gridSize, y * gridSize, layer};
                    sprites.append(currentSprite);
                    objPos.append(pos);

                    emit objectAdded(pos.x, pos.y, pos.z);
                } else {
                    if(objs == -1)
                        return;

                    sprites.removeAt(objs);
                    objPos.removeAt(objs);

                    emit objectRemoved(objs);
                }

                update();
                return;
            }
        }
    }
}


void GCRoomWidget::addObject(QImage sprite, int x, int y, int z)
{
    PosXYZ pos = {x, y, z};
    sprites.append(sprite);
    objPos.append(pos);
    update();
}
